package com.zyz.K_集合.Set接口实现类三TreeSet;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author: vick
 * @Date: 2025/3/30
 * @Description:
 * @version: 1.0
 */
public class Demo3 {
    public static void main(String[] args) {
        Comparator comparator = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof User2 && o2 instanceof User2) {
                    User2 user1 = (User2) o1;
                    User2 user2 = (User2) o2;
                    return user1.getName().compareTo(user2.getName());
                }
                throw new RuntimeException("类型不匹配");
            }
        };
        TreeSet<User2> set = new TreeSet<>(comparator);

        set.add(new User2("Tom", 12));
        set.add(new User2("vick", 22));
        set.add(new User2("lili", 32));
        set.add(new User2("nana", 42));
        set.add(new User2("lulu", 12));


        Iterator<User2> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
